<?php

/**
* alltosun.com 活动控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-8-11 下午12:28:28 $
* $Id$
*/

class Action
{
    private $per_page = 20;

    //眼镜报名
    public function __call($action = '', $params = array())
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $is_excel      = Request::Get('is_excel', 0);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();
        if (isset($search_filter['name']) && $search_filter['name']) {
            $filter['`name` LIKE '] = '%'.$search_filter['name'].'%';
        }
        $filter['status'] = $status;
        if($is_excel) {
            $newglass_list = _model('newglass_apply')->getList($filter);
            $this->export_excel($newglass_list, '眼镜报名表单');
        }
        $newglass_list = array();
        $newglass_count = _model('newglass_apply')->getTotal($filter);
        if ($newglass_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($newglass_count)) {
                Response::assign('pager', $pager);
            }
            $newglass_list = _model('newglass_apply')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
        }

        Response::assign('newglass_list', $newglass_list);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::display('admin/newglass_apply_list.html');
    }

    /**
     * 删除
     * @return array
     */
    public function newglass_delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $upload_info = _uri('newglass_apply', (int)$v);
            if (!$upload_info) {
                continue;
            }

            if (1 == $upload_info['status']) {
                _model('newglass_apply')->update($v, array('status'=>0));
            } else {
                _model('newglass_apply')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 更新状态
     * @return array
     */
    public function newglass_change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $upload_info = _uri('newglass_apply', $id);
        if ($status == $upload_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('newglass_apply')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    /**
     * 优惠券导出
     */
    public function export_excel($list = array(), $title)
    {
        //初始化excel对象
        $objPHPExcel = new PHPExcel();
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
        //设置当前的sheet索引，用于后续的内容操纵
        $objPHPExcel->setActiveSheetIndex(0);
        $objActSheet = $objPHPExcel->getActiveSheet();
        // 哪几列
        $col_arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K');
        // 第一排的标题
        $col_title_arr = array('姓名', '联系方式', '地址', '工作', '年龄', '微信号', '微博号', '活动', '文章', '行业看法', '专业性内容');
        // 设置文档属性
        foreach ($col_arr as $k => $v) {
            //设置4个列的宽度
            if (in_array($v, array('A','B','C','D','E','F','G'))) {
                $objActSheet->getColumnDimension($v)->setWidth(20);
            } else {
                $objActSheet->getColumnDimension($v)->setWidth(50);
            }
            //设置单位格内容A1,B1,C1,D1
            $objActSheet->setCellValue($v."1", $col_title_arr[$k]);
            //格式化数字为字符串，避免导致数字默认右对齐单元格
            $objActSheet->getStyle($v)->getNumberFormat()->setFormatCode("@");
            //设置自动换行
            $objActSheet->getStyle($v)->getAlignment()->setWrapText(true);
        }
        foreach ($list as $k=>$v) {
            $objActSheet->setCellValue('A'.($k+2), $v['name']);
            $objActSheet->setCellValue('B'.($k+2), $v['mobile']);
            $objActSheet->setCellValue('C'.($k+2), $v['address']);
            $objActSheet->setCellValue('D'.($k+2), $v['job']);
            $objActSheet->setCellValue('E'.($k+2), $v['age']);
            $objActSheet->setCellValue('F'.($k+2), $v['wxnumber']);
            $objActSheet->setCellValue('G'.($k+2), $v['sinanumber']);
            $objActSheet->setCellValue('H'.($k+2), $v['activity']);
            $objActSheet->setCellValue('I'.($k+2), $v['essay']);
            $objActSheet->setCellValue('J'.($k+2), $v['viewpoint']);
            $objActSheet->setCellValue('K'.($k+2), $v['professional']);
        }

        //设置页面类型
        header("Content-Type: application/vnd.ms-excel");
        //用于下载的文件头
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Type: application/octet-stream");

        //$title = trim($catebirthday_gift_recordry_name."表").".xls";
        $title = date('Y-m-d').$title.".xls";
        // ff正常，ie不正常，下面是从网站找到的解决方案
        if (preg_match('/MSIE/',$_SERVER['HTTP_USER_AGENT'])) {
            $title = rawurlencode($title);
        }
        header("Content-Disposition: attachment; filename=".$title);
        header("Content-Transfer-Encoding: binary");
        //最后修改时间
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        //不使用缓存
        header('Cache-Control: ust-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: public');
        header("Expires: 0");
        header("Pragma: public");
        $objWriter->save('php://output');
        exit;
    }
}
?>